#!/bin/bash

# log file
# LOG_FILE="/tmp/htcondor_controller.log"
LOG_FILE="/dev/null"

# fetch the value of START classad
function fetch_condor_config(){
    config_file=$1
    start_value=""
    if [[ -f $config_file ]];
    then
        start_value=`/bin/grep -E "^START =" ${config_file} |/bin/awk -F' *= *' '{print $2}'`
        rc=$?
        if [[ $rc -ne 0 ]];
        then
            echo "ERROR: Failed to fetch START classad." >> ${LOG_FILE}
            exit 2
        fi
    fi
    echo ${start_value}
}

# allow the current startd matching jobs
function switch_on(){
    config_file=$1
    /bin/rm -rf ${config_file}
    /usr/sbin/condor_reconfig
}

# deny the current startd matching jobs
function switch_off(){
    config_file=$1
    echo "START = False" > ${config_file}
    /usr/sbin/condor_reconfig
}

# check the status of START classad
function check_switch(){
    config_file=$1
    if [[ -a ${config_file} ]];
    then
        echo "OFF"
    else
        echo "ON"
    fi
}

# traverse all the nagios result files
function traverse_nagios_result(){
    result_dir=$1
    status=""
    if [[ -d ${result_dir} ]];
    then
        # remove all the files these were not modified last 10 minutes
        find ${result_dir}/* -mmin +10 -exec rm -rf {} \;
        for file in `/bin/ls ${result_dir}`;
        do
            file_abs_path="${result_dir}/${file}"
            if [[ -f ${file_abs_path} ]];
            then
                sub_status=`/bin/awk 'NR==1{gsub(/^\s+|\s+$/, "");print}' ${file_abs_path}`
                rc=$?
                if [[ rc -ne 0 ]];
                then
                    echo "ERROR: Failed to fetch nagios status from ${file_abs_path}." >> ${LOG_FILE}
                    exit 3
                fi
                # record
                if [[ ${sub_status} == "err" ]];
                then
                    status="CRITICAL"
                    echo "Notice: Got nagios Critical." >> ${LOG_FILE}
                fi
            fi
        done
    fi
    echo ${status}
}

# go through all the steps of controlling htcondor
function main(){
    /bin/date >> ${LOG_FILE}
    config_file="/etc/condor/config.d/91-adervitise-switch.conf"
    nagios_result_dir="/usr/lib64/nagios/plugins/htcondor_controller/nagios_results"
    nagios_result_status=`traverse_nagios_result ${nagios_result_dir}`
    switch=`check_switch ${config_file}`
    if [[ "${nagios_result_status}" != "CRITICAL" ]] && [[ "${switch}" == "OFF" ]];
    then
        switch_on ${config_file}
        echo "Move node back to htcondor." >> ${LOG_FILE}
    elif [[ "${nagios_result_status}" == "CRITICAL" ]] && [[ "${switch}" == "ON" ]];
    then
        switch_off ${config_file}
        echo "Kick node out of htcondor." >> ${LOG_FILE}
    else
        echo "No operations need to be performed." >> ${LOG_FILE}
    fi
}

main

